﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;

namespace Factory
{
    public class dataDAO
    {
        public dataFeriado obtemData(DateTime dataRecebida)
        {
            try
            {
                DAOBase m_cnn = new DAOBase();

                #region Query
                string query =
                    @"SELECT DataNaoUtil FROM Datas WHERE DataNaoUtil=@dataRecebida";
                #endregion

                SqlCommand cmd = new SqlCommand(query, m_cnn.CreateConnection());

                cmd.Parameters.Add(new SqlParameter("dataRecebida", dataRecebida));

                SqlDataReader dr = cmd.ExecuteReader();

                IList<dataFeriado> data = ReaderToObjects(dr);

                return data.FirstOrDefault();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }

        public static IList<dataFeriado> ReaderToObjects(IDataReader reader)
        {
            if (reader.IsClosed)
            {
                throw new ArgumentException("O DataReader fornecido já estava fechado.");
            }

            int data_pos = reader.GetOrdinal("DataNaoUtil");      

            IList<dataFeriado> result = new List<dataFeriado>();

            while (reader.Read())
            {
                dataFeriado dt = new dataFeriado()
                {
                    data = reader.GetDateTime(data_pos),
                };

                result.Add(dt);
            }
            return result;
        }
    }
}
